"use strict";

const Service = require("egg").Service;

class HomeService extends Service {
  async login(con) {
    const { app, config } = this;
    const { username, pass } = con;
    // const data = await app.mysql.select('list', {
    //   where: { id },
    // });
    const data = await app.mysql.select("user", {
      where: {
        username: username,
      },
    });

    if (!data) return "该帐号不存在";
    const { password } = data;
    if (pass === password) {
      //   const token = app.jwt.sign({ foo: 'bar' }, config.jwt.secret); // 创建token
      const token = app.jwt.sign(
        {
          username,
        },
        config.jwt.secret,
        {
          expiresIn: 60,
        }
      );
      return {
        msg: "登录成功",
        token,
      };
    }
    console.log(pass, data, "我是token");
    return {
      msg: "密码错误",
    };
  }
  async index(token) {
    const { app, config } = this;
    try {
      const username = app.jwt.verify(token, config.jwt.secret).username;
      if (username == '123456') {
        return 111;
      }
      return 222;
    } catch (error) {
      return { msg: '无效token', error };
    }
  }
}

module.exports = HomeService;
